document.addEventListener('DOMContentLoaded', function() {
    // 设置当前年份
    document.getElementById('current-year').textContent = new Date().getFullYear();
    
    // 加载完成后隐藏加载动画
    window.addEventListener('load', function() {
        const loader = document.querySelector('.loader');
        loader.style.opacity = '0';
        setTimeout(() => {
            loader.style.display = 'none';
            
            // 加载实际背景图片
            loadBackgroundImages();
        }, 500);
    });
    
    // 移动菜单切换
    const mobileMenuBtn = document.querySelector('.mobile-menu-btn');
    const mainNav = document.querySelector('.main-nav');
    
    mobileMenuBtn.addEventListener('click', function() {
        this.classList.toggle('active');
        mainNav.classList.toggle('active');
    });
    
    // 平滑滚动导航
    document.querySelectorAll('.nav-link').forEach(link => {
        link.addEventListener('click', function(e) {
            if(this.hash !== '') {
                e.preventDefault();
                
                const target = document.querySelector(this.hash);
                if(target) {
                    window.scrollTo({
                        top: target.offsetTop - 80,
                        behavior: 'smooth'
                    });
                    
                    // 更新活动导航项
                    document.querySelectorAll('.nav-link').forEach(navLink => {
                        navLink.classList.remove('active');
                    });
                    this.classList.add('active');
                    
                    // 如果是移动菜单，点击后关闭
                    if(mainNav.classList.contains('active')) {
                        mobileMenuBtn.classList.remove('active');
                        mainNav.classList.remove('active');
                    }
                }
            }
        });
    });
    
    // 滚动时更新活动导航项
    window.addEventListener('scroll', function() {
        const scrollPosition = window.scrollY;
        
        document.querySelectorAll('section').forEach(section => {
            const sectionTop = section.offsetTop - 100;
            const sectionHeight = section.offsetHeight;
            const sectionId = section.getAttribute('id');
            
            if(scrollPosition >= sectionTop && scrollPosition < sectionTop + sectionHeight) {
                document.querySelectorAll('.nav-link').forEach(link => {
                    link.classList.remove('active');
                    if(link.getAttribute('href') === `#${sectionId}` || 
                       (sectionId === 'home' && link.getAttribute('href') === '#')) {
                        link.classList.add('active');
                    }
                });
            }
        });
        
        // 头部滚动效果
        const header = document.querySelector('.main-header');
        if(scrollPosition > 50) {
            header.style.background = 'rgba(255, 255, 255, 0.98)';
            header.style.boxShadow = '0 2px 15px rgba(0, 0, 0, 0.1)';
        } else {
            header.style.background = 'rgba(255, 255, 255, 0.9)';
            header.style.boxShadow = '0 2px 10px rgba(0, 0, 0, 0.1)';
        }
    });
    
    // API链接提示
    const apiLink = document.getElementById('api-link');
    if(apiLink) {
        apiLink.addEventListener('click', function(e) {
            e.preventDefault();
            alert('仓鼠API服务目前暂未开放分享，敬请期待！');
        });
    }
    
    // 项目卡片悬停效果
    const projectCards = document.querySelectorAll('.project-card');
    projectCards.forEach(card => {
        card.addEventListener('mouseenter', function() {
            const decoration = this.querySelector('.card-decoration');
            decoration.style.animation = 'rotate 5s linear infinite';
        });
        
        card.addEventListener('mouseleave', function() {
            const decoration = this.querySelector('.card-decoration');
            decoration.style.animation = 'rotate 20s linear infinite';
        });
    });
    
    // 视差效果
    if(window.innerWidth > 768) {
        document.addEventListener('mousemove', parallax);
        function parallax(e) {
            const hamster = document.querySelector('.hamster-coding');
            if(hamster) {
                const x = (window.innerWidth - e.pageX) / 50;
                const y = (window.innerHeight - e.pageY) / 50;
                hamster.style.transform = `translateX(${x}px) translateY(${y}px)`;
            }
        }
    }
});

// 加载背景图片
function loadBackgroundImages() {
    // 这里可以添加实际背景图片的加载逻辑
    // 例如使用Intersection Observer或懒加载技术
    
    // 模拟加载完成后的网格背景替换
    setTimeout(() => {
        const gridBg = document.querySelector('.grid-bg-placeholder');
        gridBg.style.background = `
            linear-gradient(to right, #eee 1px, transparent 1px),
            linear-gradient(to bottom, #eee 1px, transparent 1px),
            url('grid-pattern.jpg') center/cover no-repeat
        `;
    }, 1000);
}